package com.example.nophase.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;

import androidx.appcompat.widget.Toolbar;

import com.example.nophase.R;
import com.example.nophase.activity.manager.AdultFromActivity;
import com.example.nophase.adapter.OaCommonAdultListAdapter;
import com.example.nophase.api.RequstCommonApi;
import com.example.nophase.base.BaseActivity;
import com.example.nophase.common.BaseCallBack;
import com.example.nophase.common.BaseResp;
import com.example.nophase.common.LoginManager;
import com.example.nophase.custom.WarningDialog;
import com.example.nophase.entity.HistoryVo;
import com.example.nophase.entity.OaCommonAuditBean;
import com.example.nophase.entity.SysUserVO;
import com.example.nophase.utils.HttpUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Response;

public class FlowRecordActivity extends BaseActivity {
    @BindView(R.id.applyfor_toolbar)
    Toolbar applyforToolbar;
    @BindView(R.id.adult_mag_list)
    ListView adultMagList;
    @BindView(R.id.adult_list_progressbar)
    ProgressBar adultListProgressbar;
    private OaCommonAdultListAdapter commonAdultListAdapter;
    private String position;
    private String appType;
    List<OaCommonAuditBean> commonAuditList = new ArrayList<>();
    private String activityName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flow_record);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        String processInstanceId = intent.getStringExtra("processInstanceId");
        activityName = intent.getStringExtra("activityname");
        appType = intent.getStringExtra("appType");
        loadAdultMsh(processInstanceId);
        initToolbar();


    }

    private void initToolbar() {
        applyforToolbar.setTitle("");
        //设置导航图标要在setSupportActionBar方法之后
        setSupportActionBar(applyforToolbar);
        applyforToolbar.setNavigationIcon(R.drawable.ic_left_black_24dp);
        getSupportActionBar().setHomeButtonEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case android.R.id.home:
                this.finish();
                return true;

        }
        return super.onOptionsItemSelected(item);
    }


    public void loadAdultMsh(String processInstanceId) {
        Call<BaseResp<List<HistoryVo>>> calls = HttpUtils.getInstance().getApiService(RequstCommonApi.class).lodaAdultMsg("ANDROID", processInstanceId);
        adultListProgressbar.setVisibility(View.VISIBLE);
        calls.enqueue(new BaseCallBack<BaseResp<List<HistoryVo>>>() {
            @Override
            public void onSuccess(Response<BaseResp<List<HistoryVo>>> response) throws Exception {
                adultListProgressbar.setVisibility(View.GONE);
                List<HistoryVo> list = response.body().getData();

                if (activityName.equals("流程结束") || activityName.equals("申请人申请")) {
                    for (int i = 0; i < list.size(); i++) {
                        if (list.get(i).getOaCommonAudit() != null) {
                            if (i == list.size() - 1) {
                                commonAuditList.add((list.get(i).getOaCommonAudit()));
                            }
                        }
                    }
                    if (commonAuditList.size() > 0) {
                        commonAdultListAdapter = new OaCommonAdultListAdapter(commonAuditList, FlowRecordActivity.this,appType);

                        adultMagList.setAdapter(commonAdultListAdapter);
                        commonAdultListAdapter.notifyDataSetChanged();
                        adultMagList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                            @Override
                            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                                String cause = commonAuditList.get(position).getAdvice().toString();
                                WarningDialog dialog = new WarningDialog(FlowRecordActivity.this, "审批意见", "审批意见", "关闭");
                                dialog.show();
                                dialog.setInputMsg(cause);
                                dialog.setClicklistener(new WarningDialog.ClickListenerInterface() {
                                    @Override
                                    public void doConfirm() {
                                        dialog.dismiss();
                                    }
                                });
                                return true;
                            }
                        });

                    } else {
                        adultMagList.setVisibility(View.GONE);
                    }

                } else {


                    List<OaCommonAuditBean> commonAuditList = new ArrayList<>();
                    for (HistoryVo historyVo : list) {
                        if (historyVo.getOaCommonAudit() != null) {

                            commonAuditList.add(historyVo.getOaCommonAudit());
                        }
                    }
                    if (commonAuditList.size() > 0) {

                        commonAdultListAdapter = new OaCommonAdultListAdapter(commonAuditList, FlowRecordActivity.this,appType);
                        adultMagList.setAdapter(commonAdultListAdapter);
                        commonAdultListAdapter.notifyDataSetChanged();
                        adultMagList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                            @Override
                            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                                String cause = commonAuditList.get(position).getAdvice().toString();
                                WarningDialog dialog = new WarningDialog(FlowRecordActivity.this, "审批意见", "审批意见", "关闭");
                                dialog.show();
                                dialog.setInputMsg(cause);
                                dialog.setClicklistener(new WarningDialog.ClickListenerInterface() {
                                    @Override
                                    public void doConfirm() {
                                        dialog.dismiss();
                                    }
                                });
                                return true;
                            }
                        });
                    }
                }




            }

            @Override
            public void onFailure(String message) {

            }
        });
    }
}
